package com.sg.service.base.dataassets.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.dataassets.req.QueryResourceFileDetailReq;
import com.sg.dto.base.dataassets.req.QueryResourceFileListReq;
import com.sg.entity.OmsResourceFile;
import com.sg.service.base.dataassets.MOmsResourceFileService;
import com.wicket.okrdataassets.common.dto.FieldInfo;
import com.wicket.okrdataassets.common.exception.BizException;
import com.wicket.okrdataassets.dal.mapper.mbg.OmsResourceFileMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsResourceFileServiceImpl
        extends ServiceImpl<OmsResourceFileMapper, OmsResourceFile>
        implements MOmsResourceFileService {

    @Autowired
    OmsResourceFileMapper omsResourceFileMapper;

    /**
     * 8-01-01新增资源文件[10301]
     * gen by moon at 11/27/2024, 11:59:40 PM
     */
    @Trace(operationName = "8-01-01新增资源文件")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addResourceFile(OmsResourceFile omsResourceFile) {
        omsResourceFile.setResourceFileId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsResourceFile.setResourceFileNumber(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsResourceFile.setIsValid("FALSE");
        omsResourceFile.setCreateOriginalRoleMemberId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecOriginalRoleMemberId());//CURRENT_ORIGINAL_ROLE_MEMBERID
        omsResourceFile.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsResourceFile.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsResourceFile.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsResourceFile.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsResourceFile.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsResourceFile.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsResourceFile)) {
            return omsResourceFile.getResourceFileId();
        } else {
            return "-1";
        }

    }

    /**
     * 8-01-01修改资源文件[10305]
     * gen by moon at 11/11/2024, 1:24:29 AM
     */
    @Trace(operationName = "8-01-01修改资源文件")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateResourceFile(OmsResourceFile omsResourceFile) {
        omsResourceFile.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsResourceFile.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsResourceFile);
    }

    /**
     * 8-01-01删除资源文件[10306]
     * gen by moon at 11/11/2024, 1:24:30 AM
     */
    @Trace(operationName = "8-01-01删除资源文件")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteResourceFile(String resourceFileId) {

        return super.removeById(resourceFileId);
    }

    /**
     * 8-01-01查询资源文件详情[10307]
     * gen by moon at 11/21/2024, 9:37:48 PM
     */
    @Trace(operationName = "8-01-01查询资源文件详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsResourceFile queryResourceFileDetail(QueryResourceFileDetailReq reqDto) {

        try {
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsResourceFile> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsResourceFile> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsResourceFile retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 8-01-01查询资源文件列表[10392]
     * gen by moon at 5/26/2025, 9:35:45 PM
     */
    @Trace(operationName = "8-01-01查询资源文件列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsResourceFile> queryResourceFileList(QueryResourceFileListReq reqDto) {

        try {
            QueryWrapper<OmsResourceFile> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsResourceFile.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsResourceFile.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsResourceFile.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsResourceFile>) super.list(queryWrapper)).stream().map(item -> {
                item.setResourceFileRelaPath(CommonFunctionHelper.getFilePath(item.getResourceFileRelaPath()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
